package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbFirm;
import com.bsb.base.model.BsbFirmExample;
import com.bsb.base.model.BsbRunningAccount;
import com.bsb.base.model.BsbRunningAccountExample;
import com.bsb.base.model.BsbRunningAccountExample.Criteria;
import com.bsb.base.model.BsbUserExample;
import com.bsb.biz.service.BsbFirmService;
import com.bsb.biz.service.BsbRunningAccountService;
import com.bsb.biz.service.BsbUserService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbRunningAccount")
public class BsbRunningAccountController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbRunningAccount/list.do";
	
	@Resource
	private BsbRunningAccountService bsbRunningAccountService;
	@Resource
	private BsbUserService bsbUserService;
	@Resource
	private BsbFirmService bsbFirmService;
	
	@RequiresPermissions("bsbRunningAccount:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		String productId = (String) params.get("productId");
		String uuid = (String) params.get("uuid");
		String goodsType = (String) params.get("goodsType");
		String name = (String) params.get("name");
		String tradeType = (String) params.get("tradeType");
		String firm = (String) params.get("firm");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		BsbRunningAccountExample bsbRunningAccountExample = new BsbRunningAccountExample();
		Criteria criteria = bsbRunningAccountExample.createCriteria();
		if(StringUtils.isNotEmpty(productId)){
			criteria.andProductIdEqualTo(productId);
		}
		if(StringUtils.isNotEmpty(uuid)){
			criteria.andUuidEqualTo(uuid);
		}
		if(StringUtils.isNotEmpty(goodsType)){
			criteria.andGoodsTypeEqualTo(goodsType);
		}
		BsbUserExample userExample = new BsbUserExample();
		com.bsb.base.model.BsbUserExample.Criteria userCriteria = userExample.createCriteria();
		if(StringUtils.isNotEmpty(name)){
			userCriteria.andNameLike("%"+name+"%");
		}
		if(StringUtils.isNotEmpty(tradeType)){
			criteria.andTradeTypeEqualTo(tradeType);
		}
		BsbFirmExample firmExample = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria firmCriteria = firmExample.createCriteria();
		if(StringUtils.isNotEmpty(firm)){
			firmCriteria.andNameLike("%"+firm+"%");
		}
		if(null != startDate){
			criteria.andPaySucTimeGreaterThanOrEqualTo(startDate);
		}
		if(null != endDate){
			endDate = DateUtils.addDays(endDate, 1);
			criteria.andPaySucTimeLessThanOrEqualTo(endDate);
		}
		Long firmId = UserUtils.currentUser().getFirmId();
		if(10000 != firmId){
			criteria.andFirmIdEqualTo(firmId.intValue());
			criteria.andTradeTypeEqualTo("1");
		}
		int totalCount = bsbRunningAccountService.countRunningAccount(bsbRunningAccountExample,userExample,firmExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbRunningAccountExample.setPageIndex((pageNum - 1) * pageSize);
		bsbRunningAccountExample.setPageSize(pageSize);
		bsbRunningAccountExample.setOrderByClause("paySucTime DESC");
		List<BsbRunningAccount> bsbRunningAccounts = bsbRunningAccountService.getPageRunningAccount(bsbRunningAccountExample,userExample,firmExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbRunningAccounts", bsbRunningAccounts);
		model.addAttribute("productId", productId);
		model.addAttribute("uuid", uuid);
		model.addAttribute("goodsType", goodsType);
		model.addAttribute("name", name);
		model.addAttribute("tradeType", tradeType);
		model.addAttribute("firm", firm);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		return "/BsbRunningAccount/list";
	}
	
	@RequiresPermissions("bsbRunningAccount:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbRunningAccount/add";
	}

	@RequiresPermissions("bsbRunningAccount:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbRunningAccount") BsbRunningAccount bsbRunningAccount) {
		bsbRunningAccountService.addBsbRunningAccount(bsbRunningAccount);
		return redirect;
	}
	
	@RequiresPermissions("bsbRunningAccount:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("uuid") String uuid) {
		bsbRunningAccountService.delBsbRunningAccountByUuid(uuid);
		return redirect;
	}
	
	@RequiresPermissions("bsbRunningAccount:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("uuid") String uuid) {
		BsbRunningAccount bsbRunningAccount = bsbRunningAccountService.getBsbRunningAccountByUuid(uuid);
		model.addAttribute("bsbRunningAccount", bsbRunningAccount);
		return "/BsbRunningAccount/edit";
	}

	@RequiresPermissions("bsbRunningAccount:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbRunningAccount") BsbRunningAccount bsbRunningAccount) {
		bsbRunningAccountService.updateBsbRunningAccount(bsbRunningAccount);
		return redirect;
	}
	
	@RequiresPermissions("bsbRunningAccount:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbRunningAccount bsbRunningAccount = bsbRunningAccountService.getBsbRunningAccountByUuid(uuid);
		String userId = bsbRunningAccount.getUserId()==null?"":bsbRunningAccount.getUserId();
		Integer firmId = bsbRunningAccount.getFirmId()==null?0:bsbRunningAccount.getFirmId();
		String userNameById = bsbUserService.getUserNameById(userId)==null?"":bsbUserService.getUserNameById(userId);
		BsbFirm bsbFirmByAccount = bsbFirmService.getBsbFirmByAccount(firmId);
		if(null != bsbFirmByAccount){
			bsbRunningAccount.setFirmName(bsbFirmByAccount.getName());
		}
		if("10000".equals(userId)){
			bsbRunningAccount.setUsername("解税宝");
		}else{
			bsbRunningAccount.setUsername(userNameById);
		}
		model.addAttribute("bsbRunningAccount", bsbRunningAccount);
		return "/BsbRunningAccount/view";
	}
}
